package cn.nexd.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.nexd.app.arsenal.R;
import cn.nexd.app.model.ExpandableModel;
import cn.nexd.app.model.MallModel;


public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<ExpandableModel> expandableModels;
    private LayoutInflater mInflater;

    public MyExpandableListAdapter(Context context, LayoutInflater mInflater) {
        this.context = context;
        this.mInflater = mInflater;
        this.expandableModels = new ArrayList<ExpandableModel>();
    }

    public void addMalls(ArrayList<ExpandableModel> expandableModels) {
        this.expandableModels.clear();
        this.expandableModels.addAll(expandableModels);
        this.notifyDataSetChanged();
    }

    @Override
    public MallModel getChild(int groupPosition, int childPosition) {
//        return children[groupPosition][childPosition];
        return expandableModels.get(groupPosition).getMallModels().get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
//        return children[groupPosition].length;
        return expandableModels.get(groupPosition).getMallModels().size();
    }


    public CheckedTextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 100);

        CheckedTextView checkedTextView = (CheckedTextView) LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_checked, null);
        checkedTextView.setLayoutParams(lp);
//        android.R.layout.simple_list_item_checked
        // Center the text vertically
        checkedTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        checkedTextView.setPadding(36, 0, 0, 0);
        checkedTextView.setTextColor(Color.BLACK);
        return checkedTextView;
    }

    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        CheckedTextView textView = getGenericView();
        textView.setText(getChild(groupPosition, childPosition).getMallName());
        if (getChild(groupPosition, childPosition).isSelected()) {
            textView.setChecked(true);
        } else {
            textView.setChecked(false);
        }
        return textView;
    }

    public Object getGroup(int groupPosition) {
        return expandableModels.get(groupPosition).getCityName();
    }

    public int getGroupCount() {
        return expandableModels.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }


    /**
     * create group view and bind data to view
     */
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            v = mInflater.inflate(R.layout.list_item, null);
        } else {
            v = convertView;
        }
        TextView textView = (TextView) v.findViewById(R.id.textView);

        textView.setText(getGroup(groupPosition).toString());
        return v;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }
}